<?php
namespace plugins\goods\model;

use think\Model;
use think\Db;

/**
 * 会员模型
 */
class GoodsFootprintModel extends Model
{

	/**
     * 足迹添加
     * @param uid 用户id
     * @param type 操作类型 1添加 2删除
     * @param id  添加操作时输入商品信息array、删除操作时输入足迹id
     */
    public function footprintOperation($uid = null, $info = null)
    {


        if (empty($uid) || empty($info)) {
            return zy_sys_echo(false, '未获取到数据', '', 100);
        }

        $time = time();
        //添加足迹
        $data = [
            'uid'            => $uid,
            'goodsid'        => $info['id'],
            'title'          => $info['goods_name'],
            'thumb'          => $info['goods_img'],
            'price'          => $info['shop_price'],
            'create_time'    => $time,
            'footprint_time' => strtotime(date('y-m-d 01:00:00', $time)),
        ];
        
        $foot = db('goods_footprint')->where(['goodsid' => $data['goodsid'], 'uid' => $data['uid']])->find()['id'];
        if ($foot) {
            $res = db('goods_footprint')->where('id', $foot)->update($data);
        } else {
            $res = db('goods_footprint')->insert($data);
        }
        if ($res) {
            return zy_sys_echo(true, '成功', '', 200);
        }

        return zy_sys_echo(false, '失败', '', 101);
    }

}